﻿/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
 * Description:
 * Author: 辛爱国 x30005137
 * Create: 2020-07-28
 * Notes: 无
 */
#pragma once
#include <QtWidgets/QWidget>
#include <QFrame>
#include <qlist>
#include <QTimer>
#include <QResizeEvent>
#include <qscreen.h>
#include "ui_mainwindow.h"
#include "settingdialog.h"
#include "collectlogdialog.h"
#include "aboutdialog.h"
#include "errortoast.h"
#include "settingmenudialog.h"
#include "serverdialog.h"
#include "trayicon.h"
#include "exitconffirmdialog.h"
#include "toscreencodeunlimit.h"
#include <QResizeEvent>
#include <qscreen.h>
#include <QMutex>
#include "upgradedialog.h"
#include "certupdatedialog.h"
#include "IShareServiceController.h"
#include "ShareEventHandler.h"
#if defined(IDEASHAREKEY)
#include "toastdialog.h"
#endif
#include "HWDialog.h"
#include "INotifyUI.h"

enum APP_STATUS {
    READY = 0, // 主界面
    CONNECTING = 1, // 主界面
    CONNECTED = 2, // 共享界面 连接态
    STARTSHARING = 3, // 共享界面 连接态
    SHARING = 4, // 共享界面 共享态
    STOPSHARING = 5, // 共享界面 共享态
    DISCONNECTING = 6  // 共享界面 连接态/共享态
};

class MainWindow : public HWDialog,public INotifyUI,
    public Ui::MainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget *parent = Q_NULLPTR);
    ~MainWindow();

    void init();
	void InitUI();
    void save();
    void keyPressEvent(QKeyEvent *event);
    void handleConnectRet(bool, IDEASHARE_ERROR_ID);
    void handleDisConnectRet(bool result, IDEASHARE_ERROR_ID errcode);
    void handleSendAuxRet(bool result, IDEASHARE_ERROR_ID errcode);
    void handleStopAuxRet(bool result, IDEASHARE_ERROR_ID errcode);
    void handleCollectLogRet(bool result, IDEASHARE_ERROR_ID errcode);
    void handleDisConnectFromTerm(bool result, IDEASHARE_ERROR_ID errcode);
    void handleSendAuxStatus(bool result, IDEASHARE_ERROR_ID errcode);
    void handleTerminalInfo(bool result, IDEASHARE_ERROR_ID errcode);
    void handleUpgrageInfo(bool result, IDEASHARE_ERROR_ID errcode);
    void handleCertUpdate(bool result, IDEASHARE_ERROR_ID errcode);
    void handleSupportConferenceCtrlRet(bool result, IDEASHARE_ERROR_ID errcode);
    void handleConferenceCtrlRet(bool result, IDEASHARE_ERROR_ID errcode);
    void handleConferenceCtrlUpdateRet(bool result, IDEASHARE_ERROR_ID errcode);
#if defined(IDEASHAREKEY)
    void handleKeyClickSignal(bool result, IDEASHARE_ERROR_ID errcode);
    void handleUsbBrokenSignal(bool result, IDEASHARE_ERROR_ID errcode);
    void handleKeyConnectRet(bool result, IDEASHARE_ERROR_ID errcode);
    void handleKeyWifiStateRet(bool result, IDEASHARE_ERROR_ID errcode);
    void handleKeyUnpairSignal(bool result, IDEASHARE_ERROR_ID errcode);
#endif
    /* ------------------------- INotifyUI ----------------*/
    virtual void HandleConnectResult(std::string deviceName);
    virtual void HandleDisConnectResult();
    virtual void HandleStartSharingResult();
    virtual void HandleStopSharingResult();
    virtual void HandleGetRemoteSericeStatusResult(RemoteServiceStatus *remoteServiceStatus);
    virtual void HandleUpdateMicMuteNotify(bool isMute);
    virtual void HandleUpdateSpeakerMuteNotify(bool isSpeakerMute);
    virtual void HandleUpdateCameraMuteNotfiy(bool isCameratMute);
    virtual void HandleUpdateSpeakerVolumeNotify(int volume);
    virtual void HandleUpdateChairManNotify(bool isChairMan);
    virtual void HandleDevConfStatusNotify(ConfSate confState,AuxState auxState);
    virtual void HandleServerStopShare(ShareAppErr reason);
    virtual void HandleServerDisconnect(ShareAppErr reason);
    virtual void HandleError(ShareAppErr errorCode);
    virtual void HandleLeaveConference();
    virtual void HandleEndConference();
    /* ------------------------- Handle UI Event ----------------*/
    virtual void dealOnConnect(std::string  deviceName);
    virtual void dealDisconnect();
    virtual void dealOnSharingDisplay();
    virtual void dealOnSharingStop();
    virtual void dealError(ShareAppErr errorCode);
    virtual void dealUpdateSliderVolume(int volume);
    virtual void dealServerStopShare();
    virtual void dealServerDisconnect();
public:
    /* ------------------------- ideashare controller ----------------*/
    Huawei::Idea::ShareServiceController* mShareServiceController;
    ShareEventHandler* mshareEvtHandler;
    /* ------------------------- IDEASHAREKEY ---------------------- */
#if defined(IDEASHAREKEY)
    bool showIdeaShareKey();
#endif
    /* ------------------------- IDEASHARE --------------------------*/
    void showMainWindow();
    void showShareWindow();
    // 显示遥控器界面
    void showRemoteControlWindow();
    // 显示静音按键，当前非静音模式
    void showMuteWindow();
    // 显示解除静音界面，当前静音模式
    void showUnMuteWindow();
    // 显示视频界面，当前正在视频
    void showVideoWindow();
    // 显示打开视频，当前视频关闭
    void showOpenVideoWindow();
    // 静音声音
    void showMuteVoiceWindow();
    // 解除静音声音
    void showUnMuteVoiceWindow();
    // 是否是基础版本终端 基础版本需要去掉摄像头控制
    void setRemoteControlBaseVersion(bool);

    void showSendWindow();
    void showStopWindow();

    void showEvent(QShowEvent *event);
    void paintEvent(QPaintEvent *event);
    // 为音量条重写
    bool eventFilter(QObject *obj, QEvent *event);

    /*  type = 0 : 0 - 21 转 1 - 203
        type = 1 : 1 - 203 转 0 - 21  */
    uint32_t valueRangeConversion(uint32_t value, uint32_t type);

    bool clickMorePushbutton();	
    void SetToScreenCodeMode(bool mode);
    bool GetFontszie(QFont &font, QString &text, int &fontSize);
    QString stdWToQString(const std::wstring &str)
    {
#ifdef _MSC_VER
        return QString::fromUtf16((const ushort *)str.c_str());
#else
        return QString::fromStdWString(str);
#endif
    }
signals:
    void closeCollectLogDialog();
    void messageNotify(uint32_t, bool, uint32_t);
    /* 通过状态码的迁移改变界面状态 */
    // void signalsChangeStatus(APP_STATUS status);
    // void signalsShowErrorToast(AIR_E_ERROR_ID errcode);
public slots:
    void handleConnectTimeout();
    void messageHandler(uint32_t, bool, uint32_t);
    /* 托盘 */
    void systray_activated(QSystemTrayIcon::ActivationReason);
    // 设置
    void on_pushButtonMore_clicked();
    // 退出
    void on_pushButtonExit_clicked();
    // 最小化
    void on_pushButtonMin_clicked();
    // 数字键
    void on_pushButtonNum0_clicked();
    void on_pushButtonNum1_clicked();
    void on_pushButtonNum2_clicked();
    void on_pushButtonNum3_clicked();
    void on_pushButtonNum4_clicked();
    void on_pushButtonNum5_clicked();
    void on_pushButtonNum6_clicked();
    void on_pushButtonNum7_clicked();
    void on_pushButtonNum8_clicked();
    void on_pushButtonNum9_clicked();
    void on_pushButtonDelete_clicked();
    void on_pushButtonClear_clicked();
    // 选择数字个数
    void on_pushButtonChooseKeyNum_clicked();
    // 设置界面
    void slotpushButtonSetting();
    // 日志
    void slotpushButtonLogInfo();
    // 关于
    void slotpushButtonAbout();
    // 分享
    void on_pushButtonShare_clicked();
    void on_pushButtonStop_clicked();
    // 配置
	void slotpushButtonSetCode();
    void on_pushButtonDisConnect_clicked();

    // 转圈圈
    void updateWait();
    // 错误消息消失
    void ErrorDispear();
    // 音频流
    void on_checkBoxMusic_clicked(bool on);
    // 遥控器
    void on_pushButtonUnMute_clicked();
    void on_pushButtonMute_clicked();
    void on_pushButtonOpenVideo_clicked();
    void on_pushButtonVideo_clicked();
    void on_pushButtonMuter_clicked();
    void on_pushButtonUnMuter_clicked();
    void on_pushButtonLeaveTheMeeting_clicked();
    void on_pushButtonCloseRemoteControl_clicked();
    void on_pushButtonOpenRemoteControl_clicked();
    void on_pushButtonCloseRemote_clicked();
    void on_pushButtonRemote_clicked();
    // 处理滑块点击移动 完美方案
    void onSliderMouseLButtonPress(QObject *slider, QEvent *event);
    void on_horizontalSliderVolume_sliderPressed();
    void on_horizontalSliderVolume_sliderMoved();
    void on_horizontalSliderVolume_sliderReleased();
    void slotHideSettingMenu();
	void IsShowChooseKeyBtn(bool is);
    void resizeWindow();
    void processSendTimeout();
private:
	bool m_PassCodeMode = true; // 为真为6/8限制模式
    bool m_isSendingRequest = false;
    /* 是否是主席，会议忙闲*/
    bool mIsChairMan = false;
    bool m_isCompatibility = false; // 是否兼容cbc模式
    /* 连接设备名称*/
    QString mDeviceName = "";

    ConfSate mConfSate = CONF_IDLE;
    /* 扬声器音量大小*/
    int  mVolume = 0;

	//bool IsChineseOrNumber(int &Posion, QString &str);
    void ConnectInfoShow();
    void showKeyNumCount();
    bool isPointInCircle(QPoint &aimPoint, QPoint &Center, double &radius);

    QLabel* keyLabels [8];                      // 数字显示数组8个
	QFrame* m_keyLine[8];
    int m_currentIndex { 0 };                   // 从从左到右第几个数字

    std::atomic<bool> m_isSharing { false };
    bool m_useSixKeyNum { false };                    /* 投影码模式 : 0:8 位 1:6 位 2:自由输入 */

    QTimer *m_waitTimer { nullptr };                 /* 连接等待转圈定时器 */
    QTimer *m_errorTimer { nullptr };                /* 错误码定时器 */
    QTimer *m_sendTimer { nullptr };
#if !defined(IDEASHAREKEY)
    QTimer *m_connectTimeoutTimer { nullptr };       /* 连接超时定时器 */
#endif
    int m_spinIndex { 0 };
    TrayIcon *m_systray { nullptr };
    /* 非模态对话框 */
    SettingDialog *m_settingdialog { nullptr };
    CollectLogDialog *m_collectlogdialog { nullptr };
    AboutDialog *m_aboutdialog { nullptr };
	ServerDialog *m_setCodeialog { nullptr };
    SettingMenuDialog *m_settingMenudialog { nullptr };
    CertUpdateDialog *m_certUpdatedialog { nullptr };
    Upgradedialog *m_upgradedialog { nullptr };
#if defined(IDEASHAREKEY)
    bool m_isShowMainWindow { false };
    ToastDialog *m_toastdialog { nullptr };
#endif
protected:
    virtual bool event(QEvent *event) override;
};
